package servlet;

import java.net.InetAddress;
import java.net.UnknownHostException;
import javax.servlet.http.HttpServletRequest;
import org.eclipse.jetty.websocket.WebSocket;
import org.eclipse.jetty.websocket.WebSocketHandler;

import utils.Utils;

public class WsServlet extends WebSocketHandler
{

	/*@Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws javax.servlet.ServletException ,IOException
    {
		response.setContentType("text/html");
        response.setStatus(HttpServletResponse.SC_OK);
        response.getWriter().println("<html><body> HTTP service not supported </body></html>");
    };*/

    @Override
    protected WebSocket doWebSocketConnect(HttpServletRequest request, String protocol)
    {
    	InetAddress inetAddress;
    	//HttpSession session = request.getSession(true);
    	String ipAddress = null;
		try {
			inetAddress = InetAddress.getByName(request.getRemoteAddr());
	    	ipAddress = inetAddress.getHostName();
	    	Utils.out("\n-- New connection --");
	    	Utils.out("+ Host: "+ipAddress);
	    	Utils.out("+ IP: "+request.getRemoteAddr());
	    	Utils.out("--------------------\n");
	    	
		} catch (UnknownHostException e) {
			Utils.out(e.getMessage());
		}  
		
        return new SireWebSocket(ipAddress);
    }
    
}